﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ue.Extensions;

namespace Ue.Core.TreeViews
{
    public abstract class TreeNode<T> : ITreeNode<T>
        where T : TreeNode<T>
    {
        protected TreeNode(TreeView<T> treeView, T parentNode)
        {
            treeView.ArgumentNullExceptionByNullOrEmpty("treeView");

            this.TreeView = treeView;
            this.Parent = parentNode;
        }

        #region ITreeNode<T> 成员

        public virtual TreeView<T> TreeView { get; protected set; }

        public virtual T Parent { get; protected set; }

        public virtual IEnumerable<T> ChildNodes 
        {
            get
            {
                return this.TreeView.Items.Where(item =>
                {
                    return item != null
                        && item.Parent != null
                        && item.Parent.Equals(this);
                });
            }
        }

        public IEnumerable<T> SiblingNodes 
        {
            get
            {
                return this.Parent == null
                    ? this.TreeView.ChildNodes
                    : this.Parent.ChildNodes;
            }
        }

        #endregion
    }
}
